package com.library.www.dao.impl;

import com.library.www.dao.BorrowDao;
import com.library.www.entity.BeOverdue;
import com.library.www.entity.BorrowBooks;
import com.library.www.entity.ReturnTheBook;
import com.library.www.util.BaseDao;
import com.library.www.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BorrowDaoImpl extends BaseDao implements BorrowDao {
    ResultSet rs=null;

    /**
     * 借书
     * @param books
     * @return
     */
    @Override
    public int borrow (BorrowBooks books) {
//        String sql="INSERT INTO borrow(`cardId`,`bookId`,`startTime`,`day`,`phone`)VALUES\n" +
//                "(?,?,?,?,?)";
//        int i = executeUpdate(sql, books.getCardId(), books.getBookId(), books.getStartTime(), books.getDay(), books.getPhone());
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.borrow(books);
    }

//        @Test
//        public void test(){
//            SqlSession sqlSession = MybatisUtil.getSqlSession();
//            BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
//            int borrow = mapper.borrow(new BorrowBooks(15,16,"2002-01-1",5,"123456789"));
//            System.out.println(borrow);
//    }
    /**
     * 还书
     * @param theBook
     * @return
     */
    @Override
    public int returnThe (ReturnTheBook theBook) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.returnThe(theBook);
    }

    /**
     * 查询用户是否借书
     * @param map
     * @return
     */
    @Override
    public int findBorrow (Map map) {
//        int sum=0;
//        String sql="SELECT * FROM borrow WHERE cardId=? AND bookId=?";
//        rs=executeQuery(sql,carId,bookId);
//        try {
//            rs.next();
//            sum = rs.getInt(1);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);

        return mapper.findBorrow(map);
    }

    /**
     * 判断是否逾期
     * @param
     * @return
     */
    @Override
    public int findDateTime (Map map) {
//        int i=0;
//        String sql="SELECT DATEDIFF(r.endTime,b.startTime) FROM `borrow` b\n" +
//                "LEFT JOIN `repay` r ON b.bookId=r.bookId\n" +
//                "WHERE r.cardId=? AND r.bookId=?";
//      rs = executeQuery(sql,carId,bookId);
//        try {
//            rs.next();
//           i =rs.getInt(1);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);

        return mapper.findDateTime(map);
    }

    /**
     * 查询约定天数
     * @return
     */
    @Override
    public int findDay (Map map) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);

        return mapper.findDay(map);
    }

    /**
     * 新增违约
     * @param overdue
     * @return
     */
    @Override
    public int findInsert (BeOverdue overdue) {
//        String sql="INSERT INTO `record`(`cardid`,`bookid`,`startTime`,`day`,`endtime`,`type`,`money`)VALUES\n" +
//                "(?,?,?,?,?,?,?)";
//        int i = executeUpdate(sql, overdue.getCardId(), overdue.getBookId(), overdue.getStartTime(), overdue.getDay(), overdue.getEndTime(), overdue.getType(), overdue.getMoney());
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findInsert(overdue);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public int findDelete (int id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findDelete(id);
    }
//    @Test
//    public void test1(){
//        int delete = findDelete(1);
//        System.out.println(delete);
//    }
    /**
     * 违约修改
     * @param overdue
     * @return
     */
    @Override
    public int findUpdate (BeOverdue overdue) {
//        String sql="UPDATE `record` r,`breakType` b SET r.`cardid`=?,r.`bookid`=?,r.`startTime`=?,r.`day`=?,r.`endtime`=?,b.`type`=?,r.`money`=? WHERE b.id=r.`type` AND r.id=?";
//        int i = executeUpdate(sql, overdue.getCardId(), overdue.getBookId(), overdue.getStartTime(), overdue.getDay(), overdue.getEndTime(), overdue.getRetype(), overdue.getMoney(),overdue.getId());
//        System.out.print(overdue.getCardId()+""+overdue.getBookId()+""+overdue.getStartTime()+""+overdue.getDay()+""+overdue.getEndTime()+""+overdue.getRetype()+""+overdue.getMoney()+""+overdue.getId());
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findUpdate(overdue);
    }

    /**
     * 查询违约总记录数
     * @return
     */
    @Override
    public int findSelect (String name) {
//        int count=0;
//        String sql="select COUNT(1) FROM reader,record,breakType WHERE breakType.id=record.type AND reader.reaId=record.cardid";
//        if (name!=null && !name.equals("")){
//            sql+=" and reader.`name`=?";
//            rs=executeQuery(sql,name);
//        }else {
//            rs=executeQuery(sql);
//        }
//        try {
//            rs.next();
//            count=rs.getInt(1);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findSelect(name);
    }

    /**
     * 违约记录分页查询
//     * @param name
//     * @param start
//     * @param pageSize
     * @return
     */
//    String name, Integer start, Integer pageSize
    @Override
    public List<BeOverdue> findByPage (Map map) {
//        List<BeOverdue> list=new ArrayList<>();
//        String sql="select record.id,record.cardid,reader.`name`,record.bookid,record.startTime,record.`day`,record.endtime,breakType.type,record.money FROM reader,record,breakType WHERE breakType.id=record.type AND reader.reaId=record.cardid";
//        if (name!=null && !name.equals("")){
//            sql+=" and reader.`name`=? LIMIT ?,?";
//            rs=executeQuery(sql,name,start,pageSize);
//        }else{
//            sql+=" LIMIT ?,?";
//            rs=executeQuery(sql,start,pageSize);
//        }
//        try {
//            while (rs.next()){
//                BeOverdue overdue=new BeOverdue(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getInt(4),rs.getString(5),
//                        rs.getInt(6),rs.getString(7),rs.getString(8),rs.getDouble(9));
//                list.add(overdue);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findByPage(map);
    }
//    @Test
//    public void test(){
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
//        Map map=new HashMap();
//        map.put("name","陈璇");
//        map.put("start",1);
//        map.put("pageSize",5);
//        List<BeOverdue> byPage = mapper.findByPage(map);
//        byPage.forEach(a->{
//            System.out.println(a);
//        });
//    }

    /**
     * 还书总记录数
     * @param name
     * @return
     */
    @Override
    public int findRepay (String name) {
//        int count=0;
//        String sql="SELECT COUNT(1) FROM repay,borrow,reader WHERE repay.bookid=borrow.bookid AND reader.reaId=repay.cardId";
//        if (name!=null && !name.equals("")){
//            sql+=" AND reader.`name`=?";
//            rs=executeQuery(sql,name);
//        }else {
//            rs=executeQuery(sql);
//        }
//        try {
//            rs.next();
//            count=rs.getInt(1);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findRepay(name);
    }

    /**
     * 还书分页查询
//     * @param name
//     * @param start
//     * @param pageSize
     * @return
     */
    @Override
    public List<BeOverdue> findRepayPage (Map map) {
//        List<BeOverdue> list=new ArrayList<>();
//        String sql="SELECT repay.id,borrow.cardId,reader.`name`,borrow.bookId,borrow.startTime,borrow.`day`,repay.endTime,borrow.phone FROM repay,borrow,reader WHERE repay.bookid=borrow.bookid AND reader.reaId=repay.cardId";
//        if (name!=null && !name.equals("")){
//            sql+=" AND reader.`name`=? LIMIT ?,?";
//            rs=executeQuery(sql,name,start,pageSize);
//        }else {
//            sql+=" LIMIT ?,?";
//            rs=executeQuery(sql,start,pageSize);
//        }
//        try {
//            while (rs.next()){
//                BeOverdue overdue=new BeOverdue(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getInt(4),rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8));
//                list.add(overdue);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }finally {
//            Closes(conn,null,rs);
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findRepayPage(map);
    }

    /**
     * 查询借书时间
//     * @param cardId
//     * @param bookId
     * @return
     */
    @Override
    public String findStarTime (Map map) {
//        String i=null;
//        String sql="SELECT startTime FROM borrow WHERE cardId=? AND bookId=?";
//        rs = executeQuery(sql, cardId, bookId);
//        try {
//            rs.next();
//            i=rs.getString(1);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        BorrowDao mapper = sqlSession.getMapper(BorrowDao.class);
        return mapper.findStarTime(map);
    }
}
